package com.tf.xx.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tf.xx.R;
import com.tf.xx.entity.EduCourse;
import com.tf.xx.entity.vo.*;
import com.tf.xx.service.EduCourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/eduservice/course")
public class EduCourseController {
    @Autowired
    private EduCourseService eduCourseService;

    @RequestMapping("/addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoForm courseInfoForm){
        String id = eduCourseService.addCourseInfo(courseInfoForm);
        return R.ok().data("courseId", id).message("添加课程信息成功");
    }

    // 多条件分页查询
    @Operation(description = "多条件查询课程带分页")
    @PostMapping("/pageCourseCondition/{page}/{limit}")
    public R pageCourseCondition(
            @Parameter(name = "page", description = "当前页码", required = true)
            @PathVariable Long page,
            @Parameter(name = "limit", description = "每页记录数", required = true)
            @PathVariable Long limit,
            @RequestBody(required = false) CourseQuery courseQuery) { // 通过封装courseQuery对象来直接传递参数

        // 创建分页page对象
        Page<EduCourse> pageParam = new Page<>(page, limit);
        // 调用方法实现多条件分页查询
        eduCourseService.pageQuery(pageParam, courseQuery);
        // 获取查询到的数据
        List<EduCourse> records = pageParam.getRecords();
        // 获取总记录数
        long total = pageParam.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    //查询课程信息
    @RequestMapping("/getCourseInfoById/{courseId}")
    public R getCourseInfoById(@PathVariable String courseId){
        CourseInfoForm courseInfoForm = eduCourseService.getCourseInfo(courseId);
        return R.ok().data("courseInfoForm", courseInfoForm);
    }

    //查询课程信息列表
    @RequestMapping("/getCourseList/")
    public R getCourseList(){
        List<EduCourse> list = eduCourseService.list(null);
        return R.ok().data("list", list);
    }

    //修改课程信息
    @RequestMapping("/updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoForm courseInfoForm){
        eduCourseService.updateCourseInfo(courseInfoForm);
        return R.ok().message("修改课程信息成功");
    }

    //删除课程信息
    //课程列表中删除课程方法
    @DeleteMapping("/removeCourseById/{id}")
    public R removeCourseById(@PathVariable("id") String id){
        boolean flag = eduCourseService.removeCourse(id);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @GetMapping("/getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id) {
        CoursePublishVo publishCourseInfo = eduCourseService.getPublishCourseInfo(id);
        return R.ok().data("publishCourseInfo", publishCourseInfo);
    }

    //根据课程id，查询课程信息{订单}
    @PostMapping("/getCourseInfoByIdOrder/{courseid}")
    public EduCourseVo getCourseInfoByIdOrder(@PathVariable String courseid){
        CourseWebVo courseInfo = eduCourseService.getBaseCourseInfo(courseid);
        EduCourseVo eduCourseVo = new EduCourseVo();
        BeanUtils.copyProperties(courseInfo, eduCourseVo);
        return eduCourseVo;
    }

    //发布课程
    @PostMapping("/publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        eduCourse.setGmtCreate(new Date());
        eduCourse.setGmtModified(new Date());
         boolean flag = eduCourseService.updateById(eduCourse);
         if(flag){
            return R.ok().message("发布课程成功");
        }else{
             return R.error().message("发布课程失败");
        }
    }


}


